package pictureseditor;


import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import pictureseditor.images.Image;

@SuppressWarnings("serial")
public class ImagePanel extends JPanel {
	
	private Image image;

	public ImagePanel(Image image) {
		super();
		assert((image.getProfondeur()==1) || (image.getProfondeur()==3));
		this.image=image;
	}

	public void setImage(Image image) {
		assert((image.getProfondeur()==1) || (image.getProfondeur()==3));
		this.image=image;
		repaint();
	}

	public void paint(Graphics g) {
		int rx=getWidth();
		int ry=getHeight();
		double sx=image.getLargeur()/(double)rx;
		double sy=image.getHauteur()/(double)ry;
		for(int x=0;x<rx;x++) {
			for(int y=0;y<ry;y++) {
				int realx=(int)(x*sx);
				int realy=(int)(y*sy);
				if (image.getProfondeur()==1) {
					float val=(float)(image.getPixel(0, realx, realy)/255.0);
					g.setColor(new Color(val,val,val));
					g.fillRect(x, y,1,1);
				}
				if (image.getProfondeur()==3) {
					float r=(float)(image.getPixel(0, realx, realy)/255.0);
					float gg=(float)(image.getPixel(1, realx, realy)/255.0);
					float b=(float)(image.getPixel(2, realx, realy)/255.0);
					g.setColor(new Color(r,gg,b));
					g.fillRect(x, y,1,1);
				}
			}
		}
	}
} 